<template>
	<view class="mainChildAccount">
		<view class="mainChildAccount-nav">
			<view class="mainChildAccount-nav-fixed">
				<view class="mainChildAccount-nav-top">
					<view class="mainChildAccount-nav-top-status-bar"
						:style="{ height: systemInfo.statusBarHeight + 'px' }">
					</view>
					<view class="mainChildAccount-nav-top-navbar-inner"
						:style="{ height: navHeight + 'px', lineHeight:navHeight+'px' }">
						<text class="iconfont iconarrow_left-copy" @click="returnTap"></text>
						<view>子账号管理</view>
					</view>
				</view>
				<view class="mainChildAccount-top-title"
					:style="{ height: optionHeight + 'px', lineHeight:optionHeight+'px' }">
					<view class="mainChildAccount-top-title-l">
						子账号
					</view>
					<view class="mainChildAccount-top-title-r">
						<button type="default" @click="childAdd"><text class="iconfont icontianjia"></text>添加</button>
					</view>
				</view>
			</view>
			<view class="mainChildAccount-nav-placeholder"
				:style="{ height: Number(optionHeight) +  Number(navHeight)+ Number(systemInfo.statusBarHeight) + 'px' }">
			</view>
		</view>
		<view class="mainChildAccount-li" v-for="(item,index) in childLsit" :key="index">
			<view class="mainChildAccount-li-top">
				<view class="mainChildAccount-li-top-l">
					{{item.ORMTB417CA01}}
				</view>
				<view class="mainChildAccount-li-top-r">
					<button type="default" class="mainChildAccount-li-top-r-solution"
						@click="operationtap(1,item)">解绑</button>
					<button type="default" v-if="item.ORMTB417CA02==0" @click="operationtap(2,item)"
						class="mainChildAccount-li-top-r-rev">启用</button>
					<button type="default" v-if="item.ORMTB417CA02==1" @click="operationtap(2,item)"
						class="mainChildAccount-li-top-r-disable">禁用</button>
				</view>
			</view>
			<view class="mainChildAccount-li-iphone">
				手机号：{{item.ORMTB417CA00}}
			</view>
			<view class="mainChildAccount-li-switch mainChildAccount-li-border">
				<view class="">
					派单查看：
				</view>
				<view class="mainChildAccount-li-switch-r" @click="switchChange(1,item)">
					<view class="mainChildAccount-li-switch-r-mask">

					</view>
					<u-switch size="35" v-model="item.sendToView" inactive-color="#ee0a24"></u-switch>
				</view>
			</view>
			<view class="mainChildAccount-li-switch mainChildAccount-li-border">
				<view class="">
					派单操作：
				</view>
				<view class="mainChildAccount-li-switch-r" @click="switchChange(2,item)">
					<view class="mainChildAccount-li-switch-r-mask">

					</view>
					<u-switch size="35" v-model="item.sendOperation" inactive-color="#ee0a24"></u-switch>
				</view>
			</view>
			<view class="mainChildAccount-li-switch mainChildAccount-li-border">
				<view class="">
					价格查看：
				</view>
				<view class="mainChildAccount-li-switch-r" @click="switchChange(3,item)">
					<view class="mainChildAccount-li-switch-r-mask">

					</view>
					<u-switch size="35" v-model="item.priceToView" inactive-color="#ee0a24"></u-switch>
				</view>
			</view>
			<view class="mainChildAccount-li-switch mainChildAccount-li-border">
				<view class="">
					派单金额：
				</view>
				<view class="mainChildAccount-li-switch-r" @click="switchChange(4,item)">
					<view class="mainChildAccount-li-switch-r-mask">

					</view>
					<u-switch size="35" v-model="item.isMoney" inactive-color="#ee0a24"></u-switch>
				</view>
			</view>
			<view class="mainChildAccount-li-switch mainChildAccount-li-border">
				<view class="">
					扫码确认：
				</view>
				<view class="mainChildAccount-li-switch-r" @click="switchChange(5,item)">
					<view class="mainChildAccount-li-switch-r-mask">

					</view>
					<u-switch size="35" v-model="item.priceQRView" inactive-color="#ee0a24"></u-switch>
				</view>
			</view>
			<view class="mainChildAccount-li-money mainChildAccount-li-border">
				<view class="mainChildAccount-li-money-l">
					可用余额：{{item.ORMTB417CA07}}
					<!-- <label>(0为不限制)</label> -->
				</view>
				<!-- <view class="" @click="moneyUpdate(item)">
					<text class="iconfont iconxiugai"></text>
				</view> -->
			</view>
			<view class="mainChildAccount-li-money but-sty-flx">
				<u-button type="success" @click="moneyUpdate(item)">账户充值</u-button>
				<u-button type="error" @click="qypdsetFun(item)">企业派单设置</u-button>
			</view>
			
			
		</view>
		<view class="public-no-img" v-if="childLsit.length==0">
			<image :src="getStaticFilePath('/QXY/noData.png')" alt="">
		</view>
		<u-modal v-model="permissionsShow" :content="modelContent" :show-cancel-button="true"
			@confirm="permissionsConfirm" @cancel="modelCancel"></u-modal>
		<u-modal v-model="operationShow" :content="operationContent" :show-cancel-button="true"
			@confirm="operationConfirm" @cancel="modelCancel"></u-modal>
		<u-modal v-model="moneyShow" title="派单金额" :show-cancel-button="true" @confirm="moneyConfirm"
			@cancel="modelCancel">
			<view class="money-content">

				<view class="example">
					<view class="segmented-control">
						<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem"
							styleType="button">
						</uni-segmented-control>
					</view>
					<!-- 展示不同的排列方式 -->
					<!-- <uni-forms ref="baseForm" :modelValue="alignmentFormData" :label-position="alignment">
						<uni-forms-item label="姓名" required>
							<uni-easyinput v-model="baseFormData.name" placeholder="请输入姓名" />
						</uni-forms-item>
						<uni-forms-item label="年龄" required>
							<uni-easyinput v-model="baseFormData.age" placeholder="请输入年龄" />
						</uni-forms-item>
					</uni-forms> -->
				</view>
				<!-- <view class="" style="display: flex; padding-top: 40rpx;">
					<view style="text-align: center;">可用余额:</view>
					<view class="">
						{{ORMTB417CA07}}
					</view>
				</view> -->
				<view class="" style="display: flex; padding-top: 40rpx;">
					<view style="text-align: center; line-height: 80rpx;">{{textech}}:</view>
					<view class="">
						<input ref="money" type="number" v-model="ORMTB417CA07" @input="moneyIpt" @blur="topMoneyChange" />
					</view>
				</view>
				
			</view>
		</u-modal>
		<u-modal v-model="moneyindeedShow" :content="moneyindeedContent" :content-style ="conStyle" :show-cancel-button="true"
			@confirm="moneyindeedConfirm" @cancel="modelCancel"></u-modal>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				systemInfo: uni.getSystemInfoSync(),
				navHeight: '',
				optionHeight: 45,
				userinfo: JSON.parse(uni.getStorageSync('userinfo')),
				permissionsShow: false,
				checked: false,
				childLsit: [],
				modelContent: '',
				postData: '',
				ORMTB417CA07: 0,
				operationtype: '',
				operationShow: false,
				operationContent: '',
				moneyShow: false,
				moneyindeedShow: false,
				moneyindeedContent: '',
				page: 1,
				countpage: 1,
				// 分段器数据
				current: 0,
				items: ['充值', '红冲'],
				textech:'充值金额',
				conStyle:{
					color:'red'
				}
			}
		},
		onShow() {

			this.queryChild()
			this.systemInfo = uni.getSystemInfoSync()
			this.navHeight = this.systemInfo.platform == 'ios' ? 44 : 48;
		},
		onReachBottom() {
			if (this.page == this.countpage) {
				return this.status = "nomore"
			}
			this.page++
			this.queryChild()
		},
		methods: {
			returnTap() {
				uni.switchTab({
					url: "/pages/tabbar/main/index"
				})
			},
			childAdd() {
				uni.navigateTo({
					url: "/pages/mainChildAccountAdd/index"
				})
			},
			onClickItem(e) {
				// console.log(e);
				this.current = e.currentIndex
				
				if(this.current == 0){
					this.textech = '充值金额'
				}
				else{
					this.textech = '红冲金额'
				}
			},
			queryChild() {
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/orm/ormtb417/selectPageList',
					method: 'POST',
					dataType: 'json',
					data: {
						page: this.page,
						limit: 10,
						ORMTB410ID: this.userinfo.ORMTB410ID,
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							if (!res.data.data || res.data.data == null) {
								this.childLsit = []
								this.page = 1
								this.countpage = 1
								return
							}
							this.countpage = Math.ceil(res.data.count / 10)
							var data = res.data.data || []
							data.forEach(item => {
								item.sendToView = item.ORMTB417CA03 == 1 ? true :
									false; //派单查看授权0/1(0自己1全部)
								item.sendOperation = item.ORMTB417CA05 == 1 ? true : false; //派单操作授权0/1
								item.priceToView = item.ORMTB417CA06 == 1 ? true : false; //价格查看授权0/1
								item.isMoney = item.ORMTB417CA08 == 1 ? true : false; //价格查看授权0/1
								item.priceQRView = item.ORMTB417CA09 == 1 ? true : false; //扫码确认授权0/1
							})
							if (this.page == 1) {
								this.childLsit = data
							} else {
								this.childLsit = [...this.childLsit, ...data]
							}

						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
					},
					fail: (res) => {
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
					}
				});
			},
			operationtap(type, item) {
				this.operationtype = type
				this.postData = item
				this.operationShow = true
				if (type == 1) {
					this.operationContent = `请确认是否解绑该:${item.ORMTB417CA01}子账号`
				} else if (type == 2) {
					var str = item.ORMTB417CA02 == 0 ? '启用' : '禁用'
					this.operationContent = `请确认是否${str}该:${item.ORMTB417CA01}子账号`
				}
			},
			isDisable(item) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				var type = item.ORMTB417CA02 == 0 ? 1 : 0
				var str = type == 1 ? '启用' : '禁用'
				uni.request({
					url: this.baseUrl + '/orm/ormtb417/updateORMTB417CA02',
					method: 'POST',
					dataType: 'json',
					data: {
						ORMTB417ID: item.ORMTB417ID, //子账号信息id
						ORMTB417CA02: type, //1启用0禁用
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							this.queryChild()
							this.$refs.uToast.show({
								title: '已' + str,
								type: 'default',
								icon: false
							})
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
						this.postData = ''
					},
					fail: (res) => {
						this.postData = ''
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
					}
				});
			},
			operationConfirm() {
				if (this.operationtype == 1) {
					this.childDEl(this.postData)
				} else if (this.operationtype == 2) {
					this.isDisable(this.postData)
				}
				this.operationShow = false
			},

			childDEl(item) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				var type = item.SETTB116CA02 == 0 ? 1 : 0
				uni.request({
					url: this.baseUrl + '/orm/ormtb417/delete',
					method: 'POST',
					dataType: 'json',
					data: {
						ORMTB417ID: item.ORMTB417ID, //子账号信息id
						ORMTB417CA00: item.ORMTB417CA00 //子账号手机号
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							this.queryChild()
							this.$refs.uToast.show({
								title: '解绑成功',
								type: 'default',
								icon: false
							})
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
						this.postData = ''
					},
					fail: (res) => {
						this.postData = ''
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
					}
				});
			},
			permissionsConfirm() {
				this.isAuthorization()
				this.permissionsShow = false
			},
			moneyConfirm() {
				this.moneyindeedContent = `请确认${this.postData.ORMTB417CA01}派单${this.textech}为:${this.ORMTB417CA07}`
				
				this.moneyindeedShow = true
			},
			moneyindeedConfirm() {
				if (this.ORMTB417CA07 === '') {
					uni.showToast({
						title: '请输入派单金额',
						duration: 5000,
						icon: 'none',
					});
					return
				}
				if (this.ORMTB417CA07 >= 99999999) {
					uni.showToast({
						title: '设置金额超过最大限制99999999',
						duration: 5000,
						icon: 'none',
					});
					return
				}
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/orm/ormtb417/update',
					method: 'POST',
					dataType: 'json',
					data: {
						type:this.current,
						ORMTB417ID: this.postData.ORMTB417ID, //子账号信息id
						ORMTB417CA07: this.ORMTB417CA07
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							this.queryChild()
							this.$refs.uToast.show({
								title: '设置成功',
								type: 'default',
								icon: false
							})
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
						this.postData = ''
						this.ORMTB417CA07 = 0
					},
					fail: (res) => {
						this.postData = ''
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
					}
				});
			},
			modelCancel() {
				this.postData = ''
				this.ORMTB417CA07 = 0
			},
			moneyUpdate(item) {
				if (item.isMoney == false) {
					this.$refs.uToast.show({
						title: '金额限制为否，不可操作！！！',
						type: 'default',
						icon: false
					})
					return
				}
				this.ORMTB417CA07 = item.ORMTB417CA07
				this.postData = item
				this.moneyShow = true
			},
			qypdsetFun(item){
				console.log(item)
				const encodedData = encodeURIComponent(JSON.stringify(item));
				uni.navigateTo({					url: "/pages/mainChildAccountqypdset/index?item="+encodedData				})
				
			},
			switchChange(type, item) {
				this.permissionsShow = true
				if (type == 1) {
					var str = item.sendToView == true ? '授权后只可查看自己所派订单' : '授权后可查看全部订单'
					this.modelContent = `请确认是否为该账号:${item.ORMTB417CA01}授权派单查看权限;${str}`
					var ORMTB417CA03 = item.ORMTB417CA03 == 1 ? 0 : 1
					this.postData = {
						ORMTB417ID: item.ORMTB417ID, //子账户id
						ORMTB417CA03: ORMTB417CA03, //派单查看授权0/1(0自己1全部)
						SETTB116CC03: this.userinfo.PERTB005ID, //当前登录人姓名
					}
				} else if (type == 2) {
					var str = item.sendOperation == true ? '授权后不可进行派单' : '授权后可进行派单'
					this.modelContent = `请确认是否为该账号:${item.ORMTB417CA01}授权派单操作权限：${str}`
					var ORMTB417CA05 = item.ORMTB417CA05 == 1 ? 0 : 1
					this.postData = {
						ORMTB417ID: item.ORMTB417ID, //子账户id
						ORMTB417CA05: ORMTB417CA05, //派单查看授权0/1(0自己1全部)
					}
				} else if (type == 3) {
					var str = item.priceToView == true ? '授权后不可查看物料单价、派单金额' : '授权后可查看物料单价、派单金额'
					this.modelContent = `请确认是否为该账号:${item.ORMTB417CA01} 授权价格查看权限 ：${str}`
					var ORMTB417CA06 = item.ORMTB417CA06 == 1 ? 0 : 1
					this.postData = {
						ORMTB417ID: item.ORMTB417ID, //子账户id
						ORMTB417CA06: ORMTB417CA06, //派单查看授权0/1(0自己1全部)
					}
				} else if (type == 4) {
					var str = item.isMoney == true ? '授权后不限制派单总金额' : '授权后限制派单总金额'
					this.modelContent = `请确认是否为该账号:${item.ORMTB417CA01} 授权派单金额权限 ：${str}`
					var ORMTB417CA08 = item.ORMTB417CA08 == 1 ? 0 : 1
					this.postData = {
						ORMTB417ID: item.ORMTB417ID, //子账户id
						ORMTB417CA08: ORMTB417CA08, //派单查看授权0/1(0自己1全部)
					}
				} else if (type == 5) {
					var str = item.priceQRView == true ? '授权后不可代替主账号扫码确认' : '授权后可代替主账号扫码确认'
					this.modelContent = `请确认是否为该账号:${item.ORMTB417CA01} 授权扫码确认权限 ：${str}`
					var ORMTB417CA09 = item.ORMTB417CA09 == 1 ? 0 : 1
					this.postData = {
						ORMTB417ID: item.ORMTB417ID, //子账户id
						ORMTB417CA09: ORMTB417CA09, //派单查看授权0/1(0自己1全部)
					}
				}
			},
			isAuthorization() { //授权
				uni.showLoading({
					title: '加载中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/orm/ormtb417/updateORMTB417CA03',
					method: 'POST',
					dataType: 'json',
					data: this.postData,
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							this.queryChild()
							this.$refs.uToast.show({
								title: '授权成功',
								type: 'default',
								icon: false
							})
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
						this.postData = ''
					},
					fail: (res) => {
						this.postData = ''
						uni.hideLoading();
						this.$refs.uToast.show({
							title: res.errMsg,
							type: 'default',
							icon: false
						})
					}
				});
			},
			moneyIpt(e) {
				var num = this.ORMTB417CA07
				num = num.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
				num = num.replace(/^\./g, ""); //验证第一个字符是数字
				num = num.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
				num = num.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
				num = num.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
				if (num.indexOf(".") < 0 && num != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
					num = parseFloat(num);
				}
				this.ORMTB417CA07 = ''
				this.$nextTick(function() {
					this.ORMTB417CA07 = num
				})
			},
			topMoneyChange() { //充值金额失去焦点数据处理
				if (this.ORMTB417CA07 == '') {
					this.$nextTick(function() {
						this.ORMTB417CA07 = 1
						this.ORMTB417CA07 = 0
					})
				}
				var index = String(this.ORMTB417CA07).indexOf('.')
				if (index == -1) return
				var numArr = this.ORMTB417CA07.split('.')
				if (numArr[1].length == 0) {
					this.$nextTick(function() {
						this.ORMTB417CA07 = 1
						this.ORMTB417CA07 = numArr[0]
					})
				} else {
					this.$nextTick(function() {
						this.ORMTB417CA07 = 1
						this.ORMTB417CA07 = numArr[0] + '.' + numArr[1]
					})
				}
			}
		}
	}
</script>

<style scoped>
	.mainChildAccount-nav-fixed {
		position: fixed;
		top: 0;
		width: 100%;
		z-index: 999;
		background-color: #FFF;
	}

	.mainChildAccount-nav-top-navbar-inner {
		/* display: flex; */
		position: relative;
	}

	.mainChildAccount-nav-top-navbar-inner text {
		position: absolute;
		top: 0;
		left: 0;
		width: 100rpx;
		text-align: center;
		font-size: 34rpx;
		z-index: 1;
	}

	.mainChildAccount-nav-top-navbar-inner view {
		flex: 1;
		text-align: center;
		font-size: 34rpx;
		font-weight: 600;
	}

	.mainChildAccount-nav-option {
		height: inherit;
		display: flex;
		font-size: 30rpx;
		color: #333;
		text-align: center;
		overflow: hidden;
	}

	.mainChildAccount-nav-option-rep {
		position: relative;
		flex: 1;
		padding: 0 55rpx 0 20rpx;
	}

	.mainChildAccount-nav-option-rep .iconxiala {
		position: absolute;
		top: 0;
		right: 0;
		height: inherit;
		line-height: inherit;
		font-size: 22rpx;
		margin-left: 10rpx;
		color: #999;
	}

	.mainChildAccount-nav-option-screening {
		flex: .35;
	}

	.mainChildAccount-nav-option-screening .iconshaixuan {
		font-size: 30rpx;
		margin-left: 10rpx;
	}

	.mainChildAccount {
		width: 100vw;
		min-height: 100vh;
		background-color: #e3e3e3;
	}

	.mainChildAccount-top-title {
		display: flex;
		justify-content: space-between;
		height: 90rpx;
		background-color: #fff;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
	}

	.mainChildAccount-top-title-l {
		height: 50rpx;
		line-height: 50rpx;
		font-size: 32rpx;
	}

	.mainChildAccount-top-title-r button {
		height: 50rpx;
		line-height: 50rpx;
		background-color: #1989fa;
		color: #FFF;
		font-size: 26rpx;
	}

	.mainChildAccount-top-title-r .iconfont {
		font-size: 20rpx;
		margin-right: 10rpx;
	}

	.mainChildAccount-li {
		background-color: #FFF;
		padding: 30rpx 20rpx;
		margin-top: 5rpx;
		color: #333;
		font-size: 32rpx;
	}

	.mainChildAccount-li-top {
		display: flex;
		justify-content: space-between;
		border-bottom: 2rpx solid #e3e3e3;
		padding: 0 20rpx;
	}

	.mainChildAccount-li-top-l {
		flex: 1;
		padding-right: 20rpx;
	}

	.mainChildAccount-li-top-r button {
		display: inline-block;
		height: 45rpx;
		line-height: 45rpx;
		color: #FFF;
		font-size: 24rpx;
	}

	.mainChildAccount-li-top-r-solution,
	.mainChildAccount-li-top-r-disable {
		background-color: rgb(255, 76, 76);
	}

	.mainChildAccount-li-top-r-rev {
		background-color: rgba(0, 202, 242, 1);
	}

	.mainChildAccount-li-top-r button+button {
		margin-left: 10rpx;
	}

	.mainChildAccount-li-iphone {
		height: 70rpx;
		line-height: 70rpx;
		padding: 0 20rpx;
	}

	.mainChildAccount-li-switch {
		display: flex;
		justify-content: space-between;
		padding: 10rpx 20rpx;
	}

	.mainChildAccount-li-switch-r {
		position: relative;
	}

	.mainChildAccount-li-switch-r-mask {
		position: absolute;
		z-index: 2;
		width: 100%;
		height: 100%;
	}

	.mainChildAccount-li-border {
		border-bottom: 2rpx solid #e3e3e3;
	}

	.mainChildAccount-li-money {
		height: 70rpx;
		line-height: 70rpx;
		display: flex;
		justify-content: space-between;
		padding: 0 20rpx;
	}
	.but-sty-flx{
		height: 100%;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
	}

	.mainChildAccount-li-money-l label {
		font-size: 20rpx;
		color: red;
		margin-left: 10rpx;
	}

	.mainChildAccount-li-money .iconxiugai {
		font-size: 30rpx;
		color: #999;
	}

	.money-content {
		padding: 30rpx 40rpx;
	}

	.money-content input {
		height: 80rpx;
		line-height: 80rpx;
		border-bottom: 2rpx solid #e3e3e3;
		padding-left: 20rpx;
	}
</style>
